# -*- coding: utf-8 -*-
# @Time : 2021/9/11 22:00
# @Author : 阿怪
# @Email : 673691277@qq.com
# @File : message_page.py
# @Project : crm自动化测试项目实战
'''短信模板设置页面类'''

from selenium.webdriver.common.by import By
from page.base_page import BasePage
from time import sleep
import time

today = time.strftime('%Y-%m-%d')  # 获取当前日期
from utils.util import HOST2

class MassagePage(BasePage):
    '''短信模板设置页面类'''

    #定义定位器
    url = '{}/index.php?m=sms&a=index'.format(HOST2)
    # url = 'http://192.168.3.156/crm3/index.php?m=sms&a=index'
    add_message_locator = (By.ID, 'add')  # 添加短信模板定位器
    add_tittle_locator = (By.CSS_SELECTOR, 'input[name="subject"]')  # 短信标题定位器
    text_locator = (By.ID, 'content')    #短信内容定位器
    submit_locator = (By.NAME,'submit')  #保存编辑定位器
    # edit_locator = (
    # By.CSS_SELECTOR, '#form1 > div:nth-child(2) > table > tbody > tr:nth-child(1) > td:nth-child(4) > a') #首条短信定位器
    edit_locator = (By.XPATH,'//*[@id="form1"]/div[2]/table/tbody/tr/td[4]/a')  #首页【编辑】按钮
    message_locator = (By.XPATH,'//*[@id="form1"]/div[2]/table/tbody/tr/td[3]')  #首页第一条短信内容定位器
    checkall_locator = (By.ID, 'check_all')  #首页全选按钮定位器
    delete_loator = (By.CSS_SELECTOR,'#btn_delete')  #首页删除按钮定位器
    delete_message_locator = (By.XPATH,'/html/body/div[5]/div[2]')   #删除成功提示语定位器

    #元素操作方法
    def add_massage(self):
        '''添加短信模板'''
        element= self.driver.find_element(*self.add_message_locator)
        element.click()

    def input_tittle(self, tittle):
        '''输入标题'''
        element = self.driver.find_element(*self.add_tittle_locator)
        element.send_keys(tittle)

    def input_text(self, text):
        '''输入内容'''
        element1 = self.driver.find_element(*self.text_locator)
        element1.send_keys(text)

    def edit_message(self,tittle,text):
        '''编辑短信模板'''
        self.input_tittle(tittle)
        self.input_text(text)



    def submit_edit(self):
        '''编辑界面，点击保存'''
        element2 = self.driver.find_element(*self.submit_locator)
        element2.click()


    def edit_click(self):
        '''点击【编辑】'''
        element = self.driver.find_element(*self.edit_locator)
        element.click()

    def get_message(self):
        '''获取第一条短信的内容'''
        text = self.driver.find_element(*self.message_locator).text
        return text

    def get_del_message(self):
        '''获取删除成功信息文本'''
        text = self.driver.find_element(*self.delete_message_locator).text
        return text

    def click_checkall(self):
        '''点击全选'''
        element = self.driver.find_element(*self.checkall_locator)
        element.click()

    def click_delete(self):
        '''点击删除'''
        element1 = self.driver.find_element(*self.delete_loator)
        element1.click()

    def clear_message(self):
        '''清除短信模板'''
        self.click_checkall()
        self.click_delete()
        sleep(2)
        self.alert()






